package com.sl.auth.core.util;/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sl.auth.core.enums.SaClientTypeEnum;
import org.apache.commons.compress.utils.Lists;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;

/**
 * Sa-Token 权限认证工具类 (C端用户版)
 **/
public class StpCompositeUtil {

    private static final ThreadLocal<SaClientTypeEnum> THREAD_LOCAL = new ThreadLocal<>();

    public static void set(SaClientTypeEnum type) {
        THREAD_LOCAL.set(type);
    }

    public static SaClientTypeEnum get() {
        return THREAD_LOCAL.get();
    }


    public static void clear() {
        THREAD_LOCAL.remove();
    }


    public static String getOrgId() {
        SaClientTypeEnum saClientTypeEnum = THREAD_LOCAL.get();
        String orgId = "-1";
        if (ObjectUtil.isNull(saClientTypeEnum)) {
            return orgId;
        }
        if (saClientTypeEnum == SaClientTypeEnum.B) {
            String c = StpLoginUserUtil.getOrgId();
            if (StrUtil.isNotBlank(c)) {
                orgId = c;
            }
        }

        if (saClientTypeEnum == SaClientTypeEnum.C) {
            String c = StpClientLoginUserUtil.getOrgId();
            if (StrUtil.isNotBlank(c)) {
                orgId = c;
            }
        }
        return orgId;
    }

    public static void checkLogin(SaClientTypeEnum saClientTypeEnum) {
        if (saClientTypeEnum == SaClientTypeEnum.B) {
            StpUtil.checkLogin();
        }

        if (saClientTypeEnum == SaClientTypeEnum.C) {
            StpClientUtil.checkLogin();
        }
        StpCompositeUtil.set(saClientTypeEnum);
    }

    public static String getUserId() {
        SaClientTypeEnum saClientTypeEnum = THREAD_LOCAL.get();
        String userId = "-1";
        if (ObjectUtil.isNull(saClientTypeEnum)) {
            return userId;
        }
        if (saClientTypeEnum == SaClientTypeEnum.B) {
            String c = StpLoginUserUtil.getUserId();
            if (StrUtil.isNotBlank(c)) {
                userId = c;
            }
        }

        if (saClientTypeEnum == SaClientTypeEnum.C) {
            String c = StpClientLoginUserUtil.getUserId();
            if (StrUtil.isNotBlank(c)) {
                userId = c;
            }
        }
        return userId;
    }

    public static List<String> getLoginUserDataScope() {
        SaClientTypeEnum saClientTypeEnum = THREAD_LOCAL.get();
        if (ObjectUtil.isNull(saClientTypeEnum)) {
            return Lists.newArrayList();
        }
        if (saClientTypeEnum == SaClientTypeEnum.B) {
            return StpLoginUserUtil.getLoginUserDataScope();
        }

        if (saClientTypeEnum == SaClientTypeEnum.C) {
            return StpClientLoginUserUtil.getLoginUserDataScope();

        }
        return Lists.newArrayList();
    }

    public static String getUserName() {
        SaClientTypeEnum saClientTypeEnum = THREAD_LOCAL.get();
        String userId = StrUtil.EMPTY;
        if (ObjectUtil.isNull(saClientTypeEnum)) {
            return userId;
        }
        if (saClientTypeEnum == SaClientTypeEnum.B) {
            String c = StpLoginUserUtil.getUserName();
            if (StrUtil.isNotBlank(c)) {
                userId = c;
            }
        }

        if (saClientTypeEnum == SaClientTypeEnum.C) {
            String c = StpClientLoginUserUtil.getUserName();
            if (StrUtil.isNotBlank(c)) {
                userId = c;
            }
        }
        return userId;
    }

    public static String getAccount() {
        SaClientTypeEnum saClientTypeEnum = THREAD_LOCAL.get();
        String userId = StrUtil.EMPTY;
        if (ObjectUtil.isNull(saClientTypeEnum)) {
            return userId;
        }
        if (saClientTypeEnum == SaClientTypeEnum.B) {
            String c = StpLoginUserUtil.getAccount();
            if (StrUtil.isNotBlank(c)) {
                userId = c;
            }
        }

        if (saClientTypeEnum == SaClientTypeEnum.C) {
            String c = StpClientLoginUserUtil.getAccount();
            if (StrUtil.isNotBlank(c)) {
                userId = c;
            }
        }
        return userId;
    }

    public static void appendLogin(SaClientTypeEnum saClientTypeEnum, ServletRequestAttributes servletRequestAttributes) {
        set(saClientTypeEnum);
        RequestContextHolder.setRequestAttributes(servletRequestAttributes);
    }
}
